package com.monitor.system.service.impl;

import com.monitor.common.utils.DateUtils;
import com.monitor.common.utils.StringUtils;
import com.monitor.system.domain.MonitoringManagement;
import com.monitor.system.domain.ThresholdManagement;
import com.monitor.system.domain.vo.MonitoringManagementVo;
import com.monitor.system.mapper.MonitoringManagementMapper;
import com.monitor.system.service.IMonitoringManagementService;
import com.monitor.system.service.IThresholdManagementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import static com.monitor.common.constant.Constants.FOUR;
import static com.monitor.common.constant.Constants.ONE;
import static com.monitor.common.constant.Constants.THREE;
import static com.monitor.common.constant.Constants.TWO;
import static com.monitor.common.constant.Constants.ZERO;

/**
 * 管理Service业务层处理
 * 
 * @author monitor
 * @date 2024-06-06
 */
@Service
public class MonitoringManagementServiceImpl implements IMonitoringManagementService 
{
    @Autowired
    private MonitoringManagementMapper monitoringManagementMapper;

    @Autowired
    private IThresholdManagementService service;
    /**
     * 查询管理
     * 
     * @param id 管理主键
     * @return 管理
     */
    @Override
    public MonitoringManagement selectMonitoringManagementById(Long id)
    {
        return monitoringManagementMapper.selectMonitoringManagementById(id);
    }

    /**
     * 查询管理列表
     * 
     * @param monitoringManagement 管理
     * @return 管理
     */
    @Override
    public List<MonitoringManagement> selectMonitoringManagementList(MonitoringManagement monitoringManagement)
    {
        return monitoringManagementMapper.selectMonitoringManagementList(monitoringManagement);
    }

//    /**
//     * 查询管理列表
//     *
//     * @param monitoringManagement 管理
//     * @return 管理
//     */
//    @Override
//    public List<MonitoringManagement> selectMonitoringManagementAbnormalList(MonitoringManagement monitoringManagement)
//    {
//        return monitoringManagementMapper.selectMonitoringManagementAbnormalList(monitoringManagement);
//    }
    /**
     * 新增管理
     * 
     * @param monitoringManagement 管理
     * @return 结果
     */
    @Override
    public int insertMonitoringManagement(MonitoringManagement monitoringManagement)
    {
        monitoringManagement.setCreateTime(DateUtils.getNowDate());
        return monitoringManagementMapper.insertMonitoringManagement(monitoringManagement);
    }

    /**
     * 修改管理
     * 
     * @param monitoringManagement 管理
     * @return 结果
     */
    @Override
    public int updateMonitoringManagement(MonitoringManagement monitoringManagement)
    {
        monitoringManagement.setUpdateTime(DateUtils.getNowDate());
        return monitoringManagementMapper.updateMonitoringManagement(monitoringManagement);
    }

    /**
     * 批量删除管理
     * 
     * @param ids 需要删除的管理主键
     * @return 结果
     */
    @Override
    public int deleteMonitoringManagementByIds(Long[] ids)
    {
        return monitoringManagementMapper.deleteMonitoringManagementByIds(ids);
    }

    /**
     * 删除管理信息
     * 
     * @param id 管理主键
     * @return 结果
     */
    @Override
    public int deleteMonitoringManagementById(Long id)
    {
        return monitoringManagementMapper.deleteMonitoringManagementById(id);
    }

    /**
     * 查询abnormal管理列表
     *
     * @param monitoringManagement 管理
     * @return 管理
     */
    @Override
    public List<MonitoringManagement> selectMonitoringManagementAbnormalList(MonitoringManagement monitoringManagement)
    {
        List<MonitoringManagement> appList = monitoringManagementMapper.selectMonitoringManagementAbnormalList(monitoringManagement);
        List<MonitoringManagement> newList = new ArrayList<>();

        ThresholdManagement thresholdManagement = new ThresholdManagement();
        thresholdManagement.setType("threshold_management_hard_device");
        ThresholdManagement threshold = service.selectThresholdManagement(thresholdManagement);
        if ( appList != null && appList.size() > 0 ) {
            for (MonitoringManagement obj : appList) {

                if(StringUtils.isNotNull(threshold)) {
                    // cpu利用率 状态hardware_situation
                    BigDecimal cpuUsageRate = new BigDecimal(obj.getCapacityUsage());
                    // 内存
                    // 状态：故障
                    if((cpuUsageRate.compareTo(new BigDecimal(threshold.getExtend2())) >= 0) ) {
                        obj.setHealthStatus(FOUR);
                    }
                    // 状态：异常
                    else if((cpuUsageRate.compareTo(new BigDecimal(threshold.getExtend4())) >= 0) &&
                            (cpuUsageRate.compareTo(new BigDecimal(threshold.getExtend3())) <= 0)) {
                            obj.setHealthStatus(THREE);
                    }
                    // 状态：告警
                    else if((cpuUsageRate.compareTo(new BigDecimal(threshold.getExtend6())) >= 0) &&
                            (cpuUsageRate.compareTo(new BigDecimal(threshold.getExtend5())) <= 0)) {
                            obj.setHealthStatus(TWO);
                    }
                    // 状态：延时
                    else if((cpuUsageRate.compareTo(new BigDecimal(threshold.getExtend8())) >= 0) &&
                            (cpuUsageRate.compareTo(new BigDecimal(threshold.getExtend7())) <= 0)) {
                            obj.setHealthStatus(ONE);
                    }
                    // 状态：低于最小值 状态设为正常
                    else if((cpuUsageRate.compareTo(new BigDecimal(threshold.getExtend8())) < 0)) {
                        obj.setHealthStatus(ZERO);
                    }
                }
                monitoringManagementMapper.updateMonitoringManagement(obj);
                newList = monitoringManagementMapper.selectMonitoringManagementAbnormalList(monitoringManagement);
            }
        }
        return newList;
    }

    /**
     * 查询abnormal管理列表
     *
     * @param monitoringManagement 管理
     * @return 管理
     */
    @Override
    public List<MonitoringManagement> selectMonitoringManagementOrderByList(MonitoringManagement monitoringManagement)
    {
        return monitoringManagementMapper.selectMonitoringManagementOrderByList(monitoringManagement);
    }

    /**
     *
     *
     * @param monitoringManagement 管理
     * @return 结果
     */
    @Override
    public int selectFault(MonitoringManagementVo monitoringManagement)
    {
        return monitoringManagementMapper.selectFault(monitoringManagement);
    }

    /**
     *
     *
     * @param monitoringManagement 管理
     * @return 结果
     */
    @Override
    public int selectAbnormal(MonitoringManagementVo monitoringManagement)
    {
        return monitoringManagementMapper.selectAbnormal(monitoringManagement);
    }

    /**
     *
     *
     * @param monitoringManagement 管理
     * @return 结果
     */
    @Override
    public int selectAlarm(MonitoringManagementVo monitoringManagement)
    {
        return monitoringManagementMapper.selectAlarm(monitoringManagement);
    }

    /**
     *
     *
     * @param monitoringManagement 管理
     * @return 结果
     */
    @Override
    public int selectDelay(MonitoringManagementVo monitoringManagement)
    {
        return monitoringManagementMapper.selectDelay(monitoringManagement);
    }
}
